/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.serverpackets;

import l1j.server.server.model.L1NpcTalkData;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.npc.L1NpcHtml;

public class S_NPCTalkReturn extends ServerBasePacket {
	private static final String _S__25_TalkReturn = "[S] _S__25_TalkReturn";
	private byte[] _byte = null;

	public S_NPCTalkReturn(final int objid, final L1NpcHtml html) {
		this.buildPacket(objid, html.getName(), html.getArgs());
	}

	public S_NPCTalkReturn(final int objid, final String htmlid) {
		this.buildPacket(objid, htmlid, null);
	}

	public S_NPCTalkReturn(final int objid, final String htmlid,
			final String[] data) {
		this.buildPacket(objid, htmlid, data);
	}

	public S_NPCTalkReturn(final L1NpcTalkData npc, final int objid,
			final int action) {
		this(npc, objid, action, null);
	}

	public S_NPCTalkReturn(final L1NpcTalkData npc, final int objid,
			final int action, final String[] data) {

		String htmlid = "";

		if (action == 1) {
			htmlid = npc.getNormalAction();
		} else if (action == 2) {
			htmlid = npc.getCaoticAction();
		} else {
			throw new IllegalArgumentException();
		}

		this.buildPacket(objid, htmlid, data);
	}

	private void buildPacket(final int objid, final String htmlid,
			final String[] data) {

		this.writeC(S_OPCODE_SHOWHTML);
		this.writeD(objid);
		this.writeS(htmlid);
		if (data != null && 1 <= data.length) {
			this.writeH(0x01); // 不明バイト 分かる人居たら修正願います
			this.writeH(data.length); // 引数の数
			for (final String datum : data) {
				this.writeS(datum);
			}
		} else {
			this.writeH(0x00);
			this.writeH(0x00);
		}
	}
	public S_NPCTalkReturn(final int objid, final String htmlid,final int[] data) {
		this.buildPacket2(objid, htmlid, data);
	}
	private void buildPacket2(final int objid, final String htmlid,
			final int[] data) {

		this.writeC(S_OPCODE_SHOWHTML);
		this.writeD(objid);
		this.writeS(htmlid);
		if (data != null && 1 <= data.length) {
			this.writeH(0x01); // 不明バイト 分かる人居たら修正願います
			this.writeH(data.length); // 引数の数
//			for (final int datum : data) {
			for(int i=0 ; i<data.length; i++){
				final int datum = data[i];
				if(i !=5){
				if(datum ==1){
					this.writeS("關閉");
				}else{
					this.writeS("開啟");
				}
				}else{
					if(datum ==0){
						this.writeS("關閉");
					}else{
						this.writeS("開啟");
					}
				}
			}
		} else {
			this.writeH(0x00);
			this.writeH(0x00);
		}
	}
	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return _S__25_TalkReturn;
	}
	/**角色詳細數值專用
	 * objid , htmlid , name , int[]*/
	public S_NPCTalkReturn(final int objid, final String htmlid,final String name ,final int[] data) {
		this.buildPacketForStatus(objid, htmlid,name , data);
	}
	/**角色詳細數值專用
	 * objid , htmlid , name , int[]*/
	private void buildPacketForStatus(final int objid, final String htmlid,final String name ,final int[] data) {

		this.writeC(S_OPCODE_SHOWHTML);
		this.writeD(objid);
		this.writeS(htmlid);
		if (data != null && 1 <= data.length+1) {
			this.writeH(0x01); // 不明バイト 分かる人居たら修正願います
			this.writeH(data.length+1); // 引数の数
			this.writeS(name);
			for (final int datum : data) {
				this.writeS(Integer.toString(datum));
			}
		} else {
			this.writeH(0x00);
			this.writeH(0x00);
		}
	}
	
	public S_NPCTalkReturn(L1DummyInstance dummy)
	{
		writeC(S_OPCODE_SHOWHTML);
		writeD(dummy.getId());
		
		if (dummy.getNpcId() >= 97302 && dummy.getNpcId() <= 97305)
		{
			writeS("shopyambo");
		}
		else if (dummy.getNpcId() >= 97306 && dummy.getNpcId() <= 97311)
		{
			writeS("shopsda" + (dummy.getNpcId() - 97305));
		}
		else if (dummy.getNpcId() >= 97312 && dummy.getNpcId() <= 97313)
		{
			writeS("tagameioy");
		}
		else
		{
			writeS(null);
		}
		
		writeH(0x00);
		writeH(0x00);
	}
}
